package com.zego.wawaji.ui.center.set;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.suke.widget.SwitchButton;
import com.zego.wawaji.R;
import com.zego.wawaji.base.Constant;
import com.zego.wawaji.base.log.PSP;
import com.zego.wawaji.base.module.ModuleMgr;
import com.zego.wawaji.ui.base.BaseAppCompatActivity;
import com.zego.wawaji.ui.dialog.LoadingDialog;
import com.zego.wawaji.ui.dialog.PickerDialogUtil;
import com.zego.wawaji.ui.dialog.SimpleTipDialog;
import com.zego.wawaji.ui.play.PlayHelper;

/**
 * 设置页
 */
public class SettingAct extends BaseAppCompatActivity implements View.OnClickListener {
    private SwitchButton setting_bgm;    // 背景音乐开关
    private SwitchButton setting_voice;  // 音效开关

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting_act);
        initView();
    }

    private void initView() {
        setTitle("设置");
        setBackView();
        setting_bgm = (SwitchButton) findViewById(R.id.setting_bgm);
        setting_voice = (SwitchButton) findViewById(R.id.setting_voice);
        findViewById(R.id.check_updata).setOnClickListener(this);
        findViewById(R.id.login_out).setOnClickListener(this);
        onClickSwitchBtn();
    }

    private void onClickSwitchBtn() {
        boolean openBgm = PSP.getInstance().getBoolean(Constant.SET_BGM, true);
        boolean openVideo = PSP.getInstance().getBoolean(Constant.SET_VOICE, true);
        setting_bgm.setChecked(openBgm);
        setting_voice.setChecked(openVideo);

        setting_bgm.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SwitchButton view, boolean isChecked) {
                PSP.getInstance().put(Constant.SET_BGM, isChecked);
            }
        });

        setting_voice.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SwitchButton view, boolean isChecked) {
                PSP.getInstance().put(Constant.SET_VOICE, isChecked);
                if (isChecked) {
                    PlayHelper.playBtnSound();
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_out:
                PickerDialogUtil.showSimpleTipDialogExt(this, new SimpleTipDialog.ConfirmListener() {
                    @Override
                    public void onCancel() {
                    }

                    @Override
                    public void onSubmit() {
                        ModuleMgr.getLoginMgr().exitLogin();
                        finish();
                    }
                }, "确定退出登录吗？", "退出登录", "取消", "确定", true, R.color.text_gray);
                break;

            case R.id.check_updata:  // 检查更新
                LoadingDialog.show(this, "检查更新中...");
                ModuleMgr.getWawaMgr().reqNewVersion(true, false, null);
                break;
        }
    }
}
